import pymysql

class MysqlFuction(object):
    def __init__(self):
        # 连接数据库信息
        self.conn = pymysql.connect(
            host='211.103.136.244',
            port=7061,
            user='student',
            password='iHRM_student_2022',
            db='ihrm',
            charset='utf8'
            # autocommit='utf-8'
        )
        # 创建游标对象
        self.cur = self.conn.cursor()
        # 查询
    def mysql_cz(self,sql):
        try:
            self.cur.execute(sql)
            a = self.cur.fetchall()
            print(a)

        except Exception as e:
            print("错误是",e)
        # 修改
    def mysql_uid(self,sql):
        try:
            self.cur.execute(sql)
            self.conn.commit()
            print("操作成功")

        except Exception as e:
            self.conn.rollback()
            print("错误是",e)


    def mysql_tools(self):
        sqlyj = input("请输入sql语句")
        if 'select' in sqlyj:
            self.mysql_cz(sqlyj)

        else:
            self.mysql_uid(sqlyj)

if __name__ == '__main__':
    cz = MysqlFuction()
    cz.mysql_tools()

